# -*- coding: utf-8 -*-
import scrapy
from demo.items import CourseItem
from scrapy_redis.spiders import RedisSpider

class CourseSpider(RedisSpider):
    name = 'course'
    #allowed_domains = ['fang.5i5j.com']
    #start_urls = ['https://fang.5i5j.com/bj/loupan/']
    redis_key = "coursespider:start_urls"

    def __init__(self, *args, **kwargs):
        # Dynamically define the allowed domains list.
        domain = kwargs.pop('domain', '')
        self.allowed_domains = filter(None, domain.split(','))
        super(CourseSpider, self).__init__(*args, **kwargs)
        
    def parse(self, response):
        #print(response.status)
        '''
        hlist = response.css("div.info_right")
        
        for vo in hlist:
            item = CourseItem()
            item['title'] = vo.css("h3.fontS20 a::text").extract_first()
            item['address'] = vo.css("span.addressName::text").extract_first()
            item['time'] = vo.re("<span>(.*?)开盘</span>")[0]
            item['clicks'] = vo.re("<span><i>([0-9]+)</i>浏览</span>")[0]
            item['price'] = vo.css("i.fontS24::text").extract_first()
            #print(item)
        '''
        item = CourseItem()
        item['title'] = response.css("div.info_right h1::text").extract_first()
        #item['times'] = response.css("span.pinfo::text").extract_first()
        times = response.css("span.pinfo")
        print(times)
        item['times'] = times.re('<span class="pinfo">[0-9]+/(.*?)</span>')[0]
        item['teacher'] = response.css("div.professor_name a::text").extract_first()
        item['student'] = response.css("div.forwho span::text").extract()[1]
        item['persons'] = response.css("div.course_status span::text").extract_first()
        item['price'] = response.css("sapn.money::text").extract_first(default = '0')
        item['outline'] = response.css("span.ellipsis::text").extract()
        yield item
        #pass
